﻿using SmartMotion.Core;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Station
{
    public partial class MCDebug : UIPage
    {
        private readonly MCStation _station;

        public MCDebug(MCStation station)
        {
            _station = station;
            InitializeComponent();
        }

        private void MCDebug_Load(object sender, EventArgs e)
        {
            SetGrid();

            var points = PointsMgr.Ins.Points;
            foreach (var varInfo in points.Values)
            {
                if (varInfo.Group == _station.Name)
                {
                    List<object> rowData = new List<object> {varInfo.Name};

                    double[] point = varInfo.Value;
                    if (point == null) continue;
                    if (point.Length != _station.Axises.Count)
                    {
                        ShowErrorTip($"点位{varInfo.Name}维度与工站{_station.Name} 不相等");
                        continue;
                    }

                    rowData.AddRange(point.Cast<object>());
                    grid.Rows.Add(rowData.ToArray());
                }
            }

            for (int i = 0; i < _station.Axises.Count; i++)
            {
                tab.Controls.Add(new Label() { Text = _station.Axises[i].AxisName, AutoSize = true });
                tab.Controls.Add(new UISwitch());
                tab.Controls.Add(new UISymbolButton() { Symbol = 61470 });
                tab.Controls.Add(new UITextBox() { Type = UITextBox.UIEditType.Double });
                tab.Controls.Add(new UITextBox() { Type = UITextBox.UIEditType.Double });
                tab.Controls.Add(new UILight());
                tab.Controls.Add(new UILight());
                tab.Controls.Add(new UILight());
                tab.Controls.Add(new UILight());
                tab.Controls.Add(new UILight());
                int symbol1 = (int)SymbleDirection.Left, symbol2 = (int)SymbleDirection.Right;
                if (_station.Axises[i].AxisDirection == AxisDirection.LeftRight)
                {
                    symbol1 = (int)SymbleDirection.Left;
                    symbol2 = (int)SymbleDirection.Right;
                }
                if (_station.Axises[i].AxisDirection == AxisDirection.ForBackword)
                {
                    symbol1 = (int)SymbleDirection.Forword;
                    symbol2 = (int)SymbleDirection.Backword;
                }
                if (_station.Axises[i].AxisDirection == AxisDirection.UpDn)
                {
                    symbol1 = (int)SymbleDirection.Up;
                    symbol2 = (int)SymbleDirection.Down;
                }
                if (_station.Axises[i].AxisDirection == AxisDirection.Rotate)
                {
                    symbol1 = (int)SymbleDirection.Clockwise;
                    symbol2 = (int)SymbleDirection.Counterclockwise;
                }

                tab.Controls.Add(new UISymbolButton() { Symbol = symbol1 });
                tab.Controls.Add(new UISymbolButton() { Symbol = symbol2 });
            }
            tab.ChildrenMoveToCenter();
        }

        private void SetGrid()
        {
            grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            grid.RowHeadersVisible = false;
            grid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid.AllowUserToResizeRows = false;
            grid.MultiSelect = false;
            grid.AllowUserToAddRows = false;
            grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            DataGridViewColumn col = new DataGridViewColumn();
            col.HeaderText = "点名";
            col.CellTemplate = new DataGridViewTextBoxCell();
            grid.Columns.Add(col);

            foreach (var axis in _station.Axises)
            {
                grid.Columns.Add(new DataGridViewColumn() { HeaderText = axis.AxisName, CellTemplate = new DataGridViewTextBoxCell() });
            }
        }

        private void btnAddPos_Click(object sender, EventArgs e)
        {
            string pointName = null;
            if (UIInputDialog.InputStringDialog(ref pointName, true, "请输入点位名称"))
            {
                List<double> pos = new List<double>();
                List<object> rowData = new List<object>();

                rowData.Add(pointName);
                for (int i = 0; i < _station.Axises.Count; i++)
                {
                    pos.Add(0);
                    rowData.Add(0);
                }

                if (PointsMgr.Ins.AddPoints(pointName, _station.Name, pos.ToArray()))
                {
                    this.ShowSuccessTip("成功");
                    grid.Rows.Add(rowData.ToArray());
                }
                else
                {
                    this.ShowErrorTip("失败");
                }
            }
        }

        private void btnSavePos_Click(object sender, EventArgs e)
        {
            //把内容拿出来

            PointsMgr.Ins.Save();
        }
    }
}